import { createRouter, createWebHistory } from 'vue-router';
import { listMenu } from "@/api/bk/menu.js";
import { ref } from 'vue';

// 路由表映射
const staticRoutes = [
    {
        path: '/',
        name: 'Home',
        component: () => import('../home.vue'),
        meta: {
            showNavbar: true
        }
    },
    {
        path: '/fll/f/:id',  // 分类
        name: 'Fll',
        component: () => import('@/views/fenLei/index.vue'),
        props: true,
        meta: {
            showNavbar: true
        }
    },
    {
        path: '/article/detail/:id',  // 定义动态参数
        name: 'Detail',
        component: () => import('@/views/article/detail.vue'),
        props: true, // 启用props接收参数
        meta: {
            showNavbar: false
        }
    },
    {
        path: '/404',
        name: 'NotFound',
        component: () => import('@/views/error/404.vue'), // 确保路径正确
        meta: {
            showNavbar: false
        }
    },
    {
        path: '/zaTan',
        name: 'zaTan',
        component: () => import('@/views/zaTan/index.vue'),
        meta: {
            showNavbar: true
        }
    },
    {
        path: '/me/yl',
        name: 'yl',
        component: () => import('@/views/youLian/index.vue'),
        meta: {
            showNavbar: true
        }
    },
    {
        path: '/me/gy',
        name: 'gy',
        component: () => import('@/views/AboutView.vue'),
        meta: {
            showNavbar: true
        }
    },
    {
        path: '/coc/cocrw',
        name: 'cocrw',
        component: () => import('@/views/cocrw/index.vue'),
        meta: {
            showNavbar: false
        }
    },
]

const router = createRouter({
    history: createWebHistory(),
    routes: staticRoutes
})

// 动态路由加载函数
export const dynamicRoutes = async () => {
    try {
        // 获取菜单列表
        const response = await listMenu({ menuState: 0 });
        const menuList = response.rows || [];

        // 移除可能已存在的通配符路由
        if (router.hasRoute('NotFoundRedirect')) {
            router.removeRoute('NotFoundRedirect');
        }

        // 添加动态路由
        menuList.forEach(item => {
            // 避免重复添加路由
            if (router.hasRoute(item.menuName)) {
                router.removeRoute(item.menuName);
            }

            // 添加新路由
            router.addRoute({
                path: item.menuUrl,
                name: item.menuName,
                // 解决 Vite 生产环境动态导入问题
                component: () => {
                    // 使用更可靠的动态导入方式
                    const componentPath = `../views/${item.menuComponent}.vue`;
                    return import(/* @vite-ignore */ componentPath);
                },
                meta: { showNavbar: item.menuNavig }
            });
        });

        // 添加通配符路由 (必须放在最后)
        router.addRoute({
            path: '/:pathMatch(.*)*',
            name: 'NotFoundRedirect',
            redirect: '/404'
        });

        return true;
    } catch (error) {
        console.error('路由加载失败:', error);

        // 确保至少有一个 404 路由在出错时
        router.addRoute({
            path: '/:pathMatch(.*)*',
            name: 'NotFoundRedirect',
            redirect: '/404'
        });

        return false;
    }
};

// 初始化路由
dynamicRoutes().catch(err => {
    console.error('初始化动态路由失败:', err);
});

export default router;